function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}
const A = getApp();
var setTime;
Page({
  data: {
    src: '',
    videoDetail: {},
    comment: ''
  },
  onLoad(options) {
    let id = options.id;
    this.setData({ id })
    this.getVideo();
    this.getCommentDetailVo();
    setTime = setTimeout(()=> {
      this.addIntegraHistory();
    }, 30000)
  },
  getVideo() {
    let id = this.data.id;
    let config = {
      id
    }
    A.updata.getVideo(id).then(res => {
      console.log(res);
      this.setData({
        videoDetail: res.results
      })
      this.setVideo(this.data.videoDetail.linkUrl);
    })
  },
  setVideo(url) {
    let self = this;
    console.log(url);
    wx.showLoading({
      title: '加载中...'
    })
    if (url.indexOf('v.qq.com') !== -1) {
      var key = url.match(/http(s?):\/\/(.+?)\/(\w+?).html/)
      console.log(key)
      if (key && key[3]) {
        wx.request({
          url: 'https://h5vv.video.qq.com/getinfo?otype=json&platform=11001&sphttps=1&vid=' + key[3],
          success: function (res) {
            var data = res.data
            data = data.slice(13, data.length - 1)
            try {
              var ret = JSON.parse(data)
              console.log(ret)
              self.setData({
                is_video: true,
                video_source: ret.vl.vi[0].ul.ui[0].url + ret.vl.vi[0].fn + '?vkey=' + ret.vl.vi[0].fvkey,
                video_poster: 'https://puui.qpic.cn/qqvideo_ori/0/' + key[3] + '_496_280/0',
                video_direction: 90
              })
              wx.setNavigationBarTitle({
                title: ret.vl.vi[0].ti
              })
            } catch (e) {
              wx.showToast({
                title: '视频地址解析失败'
              })
            }
          }
        })
      }
    } else {
      this.setData({
        is_webView: true,
        url: url
      })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 1500)
  },
  // 获取积分
  addIntegraHistory() {
    let config = {
      originId: this.data.id,
      origin: 'VIDEO'
    }
    A.updata.addIntegraHistory(config).then(res => {
      console.log('获取积分');
      console.log(res);
    })
  },
  // 获取评论列表
  getCommentDetailVo() {
    // 评论类型(0：每日学习，1：教育直通车，2：视频)
    let id = this.data.id;
    let config = {
      originId: id,
      originType: 2,
      pageNum: 1,
      pageSize: 100
    }
    console.log(config);
    A.updata.getCommentDetailVo(config).then(res => {
      console.log('获取评论列表');
      console.log(res);
      this.setData({
        commentList: res.list
      })
    })
  },
  // 获取评论
  bindComment(e) {
    this.setData({
      comment: e.detail.value
    })
  },

  onfirmComment(e) {
    if (!wx.getStorageSync('userId')) {
      return wx.navigateTo({ url: '/pages/index/impower/impower', })
    }
    let value = e.detail.value;
    let config = {
      content: value,
      originType: 2,
      originId: this.data.id
    }
    A.updata.addCommentDetail(config).then(res => {
      console.log(res);
      if (res.code == 200) {
        console.log('评论成功');
        this.setData({
          comment: ''
        })
        this.getCommentDetailVo();
      }
    })
  },
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('videos')
  },
  onUnload: function() {
    clearTimeout(setTime);
  },
  pause(e) {
    console.log(e);
  },
  inputValue: '',
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  bindPlay: function () {
    this.videoContext.play();
  },
  bindPause: function () {
    this.videoContext.pause()
  },
  videoErrorCallback: function (e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  }
})